/**
 * 
 */
package org.jft.datamodel.database;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.jft.datamodel.config.Config;
import org.jft.datamodel.config.Constants;


/**
 * @author LAP
 *
 */
public class HibernateUtil {
	private SessionFactory factory = null;
	
	private HibernateUtil() {
		Configuration configuration = new Configuration();
		configuration.setProperty(AvailableSettings.DRIVER, "org.sqlite.JDBC")
			.setProperty(AvailableSettings.URL, Config.getInstance().getStringProperty(Constants.DATA_BASE_PATH_PROPERTY_NAME))
			.setProperty(AvailableSettings.POOL_SIZE, "10")
			.setProperty(AvailableSettings.DIALECT, "datamodel.database.SQLiteDialect")
			.setProperty(AvailableSettings.SHOW_SQL, "true")
			.setProperty(AvailableSettings.AUTOCOMMIT, "false")
			.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
		factory = configuration.buildSessionFactory(new ServiceRegistryBuilder()
			.applySettings(configuration.getProperties())
			.buildServiceRegistry());
	}
	
	private static class HibernateUtilCeeper {
		private static final HibernateUtil INSTANCE = new HibernateUtil(); 
	}
	
	public static HibernateUtil getInstance() {
		return HibernateUtilCeeper.INSTANCE;
	}
	
	public SessionFactory getSessionFactory() {
		return factory;
	}
	
}
